package com.jxwifi.cloud.quickclean.utils;

import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 计算时间大小工具类
 * Created by Administrator on 2019/5/20.
 */

public class TokenTimeUtils {

    private static String TAG = "TokenTimeUtils";
    /**
     * @param startTime 本身保存的时间
     * @param endTime   系统时间
     * @return false 表示不做操作 true表示做更新操作
     */
    public static boolean getTimeCompareSize(String startTime, String endTime) {
        int i = 0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");//年-月-日 时-分
        try {
            Date date1 = dateFormat.parse(startTime);//开始时间
            Date date2 = dateFormat.parse(endTime);//结束时间
            if (date1.getTime() > date1.getTime()) {//这里不做更新操作
                return false;
            } else if (date1.getTime() <= date2.getTime()) {//这里的时候做更新操作
                return true;
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 在系统的时间上多加2个小时，表示下次token需要更新的时间。
     *
     * @param context
     */
    /*public static void PreservationTime(Context context) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        //在当前的时间上加2个小时
        Date newdata = new Date(date.getTime() + 3 * 60 * 60 * 1000);
        AccountMgr.putTokenTime(context, simpleDateFormat.format(newdata));
    }*/

    /**
     * @param stardTime 服务时间
     * @param endTime   系统时间
     */
    public static String newTime(String stardTime, String endTime) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String newGetTime = "";
        boolean mIsdiff=false;
        try {
            Date d1 = df.parse(stardTime);
            Date d2 = df.parse(endTime);
            long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
            if (diff<0){
                mIsdiff=true;
            }
            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            if (days > 0) {
                newGetTime = days + "";
            } else {
                newGetTime = hours + ":" + minutes;
            }
        } catch (Exception e) {
            e.fillInStackTrace();
        }
        if (mIsdiff){
            return "00:00";
        }else{
            return newGetTime;
        }
    }

    /**
     * 得到时间差
     *
     * @return
     */
    public static boolean isContrast(String serviceTime,long times) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date(System.currentTimeMillis());
        String newGetTime = df.format(date);
        long diff = 0;
        try {
            Date d1 = df.parse(serviceTime);
            Date d2 = df.parse(newGetTime);
            diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别

        } catch (Exception e) {
            e.fillInStackTrace();
        }
        if (diff/(1000*60) > times) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 将秒数转为时分秒
     *
     * @param second
     * @return
     */
    public static String change(int second) {
        int h = 0;
        int d = 0;
        int s = 0;
        int temp = second % 3600;
        if (second >= 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp >= 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }

        if (h < 10 || d < 10) {
            if (h < 10 && d < 10) {
                return "0" + h + ":" + "0" + d + ":" + "0" + s + "";
            } else {
                if (h < 10) {
                    return "0" + h + ":" + d + ":" + "0" + s + "";
                }
                if (d < 10) {
                    return h + ":" + "0" + d + ":" + "0" + s + "";
                }
            }

        }
        return h + ":" + d + ":" + s + "";
    }

    public static String changeurs(int second) {
        int h = 0;
        int d = 0;
        int s = 0;
        String alltimeh;
        String alltimed;
        int temp = second % 3600;
        if (second >= 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp >= 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }
        if (h >= 10) {
            alltimeh = "" + h;
        } else {
            alltimeh = "0" + h;
        }
        if (d >= 10) {
            alltimed = "" + d;
        } else {
            alltimed = "0" + d;
        }


        return alltimeh + "小时" + alltimed + "分";
    }

    public static String parseDate(String dateStr) {
        String formatStr2 = "";

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");//注意格式化的表达式
        try {
            Date time = format.parse(dateStr);
            String date = time.toString();
            SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", java.util.Locale.SIMPLIFIED_CHINESE);
            Date datetime = (Date) sdf.parse(date);
            formatStr2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return formatStr2;
    }

    /**
     * 方法一 将毫秒数换算成x天x时x分x秒x毫秒
     * time 毫秒
     */

    public static String getSecondToDayHourMinutes(long ms) {

        int ss =1000;

        int mi = ss *60;

        int hh = mi *60;

        int dd = hh *24;

        long day = ms / dd;

        long hour = (ms - day * dd) / hh;

        long minute = (ms - day * dd - hour * hh) / mi;

        long second = (ms - day * dd - hour * hh - minute * mi) / ss;

        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        String strDay = /*day <10 ?"0" + day :*/"" + day;
        //Log.i(TAG,"strDay = "+strDay+" day = "+day);

        String strHour = hour <10 ?"0" + hour :"" + hour;

        String strMinute = minute <10 ?"0" + minute :"" + minute;

        String strSecond = second <10 ?"0" + second :"" + second;

        return strDay +"天:" + strHour +":" + strMinute +":" + strSecond;

    }

}
